﻿using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Forms;
using Blurift.WPFGLControl;
using Crevice2D.WPFEditorHost.Helpers;
using Crevice2D.WPFEditorHost.Infrastructure;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Crevice2D.WPFEditorHost
{
    public class BaseWindow : Window
    {
        public Mode Mode { get; set; }
        
        //Editor specifics
        public Editor _editor;

        public Stopwatch _watch;

        public BaseWindow()
        {
            _watch = new Stopwatch();

            var graphicsDevice = new GLGraphicsDevice();

            graphicsDevice.LoadContent += LoadContent;
            graphicsDevice.RenderXna += Render;
            graphicsDevice.HwndLButtonDown += HwndLButtonDown;
            graphicsDevice.HwndLButtonUp += HwndLButtonUp;
            graphicsDevice.HwndRButtonDown += HwndRButtonDown;
            graphicsDevice.HwndRButtonUp += HwndRButtonUp;
            graphicsDevice.HwndMouseMove += HwndMouseMove;
            graphicsDevice.SizeChanged += HwndSizeChanged; 
            //graphicsDevice.HwndMouseEnter += HwndMouseEnter;
            //graphicsDevice.HwndMouseLeave += HwndMouseLeave;
            graphicsDevice.Initialize("Content");
            graphicsDevice.AutoScaleMode = AutoScaleMode.None;

            RenderManager.Initialise(graphicsDevice);

            Mode = Mode.EditNodes;
        }

        #region HostWindow Event Handlers

        private void HwndSizeChanged(object sender, EventArgs e)
        {
            var viewport = new Viewport(0, 0, RenderManager.GLDevice.Width, RenderManager.GLDevice.Height);
            RenderManager.SetViewport(viewport);
        }

        public void LoadContent(object sender, GraphicsDeviceEventArgs e)
        {
            if(!_watch.IsRunning)
            {
                //DPI Awareness
                PresentationSource source = PresentationSource.FromVisual(this);
                if (source != null)
                {
                    float dpiX = (float) source.CompositionTarget.TransformToDevice.M11;
                    float dpiY = (float) source.CompositionTarget.TransformToDevice.M22;
                    PropertiesHelper.SetDPI(dpiX, dpiY);
                }

                RenderManager.LoadContent(e.GraphicsDevice);
               
                _watch.Start();
            }
        }

        public void Render(object sender, GraphicsDeviceEventArgs e)
        {
            RenderManager.Device.Clear(Color.CornflowerBlue);

            RenderManager.SpriteBatch.Begin();

            if(_editor != null)
                _editor.Draw();

            RenderManager.SpriteBatch.End();
        }

        private void HwndLButtonDown(object sender, HwndMouseEventArgs e)
        {
            _editor.MouseLDown(e);
        }

        private void HwndLButtonUp(object sender, HwndMouseEventArgs e)
        {
            _editor.MouseLUp(e);
        }

        private void HwndRButtonDown(object sender, HwndMouseEventArgs e)
        {
            _editor.MouseRDown(e);
        }

        private void HwndRButtonUp(object sender, HwndMouseEventArgs e)
        {
            _editor.MouseRUp(e);
        }

        protected virtual void HwndMouseMove(object sender, HwndMouseEventArgs e)
        {
            _editor.UpdateMouse(e);
        }

        //private void HwndMouseEnter(object sender, HwndMouseEventArgs e)
        //{
        //    Mouse.OverrideCursor = Cursors.Cross;
        //}

        //private void HwndMouseLeave(object sender, HwndMouseEventArgs e)
        //{
        //    Mouse.OverrideCursor = Cursors.Arrow;
        //}

        #endregion
    }
}